//
//  HomeViewController.m
//  Yarhoo
//
//  Created by 杨春至 on 2018/5/24.
//  Copyright © 2018年 Yarhoo. All rights reserved.
//

#import "HomeViewController.h"
#import <SDCycleScrollView.h>
#import "HomeCollectionViewCell.h"
#import "PhotoBackupsViewController.h"

static NSString *const CLCELL_IDE = @"home_collection_ide";

@interface HomeViewController ()<SDCycleScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) SDCycleScrollView *bannerView;

@property (nonatomic,strong) UICollectionView *homeCollectionView;

@property (nonatomic,strong) NSArray <NSArray*> *eqiutArray;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.eqiutArray = @[@[@"照片备份",@"视频备份",@"短信备份"],@[@"记事备份",@"通讯录备份"]];
    [self configUI];
}
- (void)configUI{
    self.view.backgroundColor = KcolorLineGray;
    [self.view addSubview:self.bannerView];
    [self.view addSubview:self.homeCollectionView];
}
#pragma mark CollectionDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = _eqiutArray[indexPath.section][indexPath.row];
    if([str isEqualToString:@"照片备份"]){
        PhotoBackupsViewController *photoVC = [[PhotoBackupsViewController alloc]init];
        photoVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:photoVC animated:YES];
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else{
        return 2;
    }
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CGFloat width = (SCREEN_WIDTH-2)/3.0;
        CGFloat height = width*1.1;
        CGSize size = CGSizeMake(width, height);
        return size;
    }else{
        CGFloat width = (SCREEN_WIDTH-1)/2.0;
        CGFloat height = width*0.7;
        CGSize size = CGSizeMake(width, height);
        return size;
    }
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    UIEdgeInsets edge = UIEdgeInsetsMake(0.2, 0.01, 0.2, 0.01);
    return edge;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0.5;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ide = [NSString stringWithFormat:@"%@%ld_%ld",CLCELL_IDE,indexPath.section,indexPath.row];
    [_homeCollectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:ide];
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ide forIndexPath:indexPath];
    [cell fillDataWithStr:_eqiutArray[indexPath.section][indexPath.row]];
    [self fillLines:cell];
    return cell;
}
- (void)fillLines:(HomeCollectionViewCell *)cell{
//    只加底部和右侧虚线
    CGFloat width = 0.4;
    CGFloat newX = cell.width - width;
    
    UIView *verticalLine = [[UIView alloc]initWithFrame:CGRectMake(newX, 0, width, cell.height)];
    [cell.contentView addSubview:verticalLine];
    
    CGFloat newY = cell.height-width;
    UIView *horizontalLine = [[UIView alloc]initWithFrame:CGRectMake(-2, newY , cell.width+2, width)];//每一个cell的
    [cell.contentView addSubview:horizontalLine];
    
    horizontalLine.backgroundColor = verticalLine.backgroundColor = KcolorLineGray;
}
- (SDCycleScrollView *)bannerView{
    if (!_bannerView) {
        _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, self.view.width, 156.0/352*self.view.width) delegate:self placeholderImage:UIImageMake(@"banner_place")];
        _bannerView.localizationImageNamesGroup = @[UIImageMake(@"banner_place"),UIImageMake(@"banner_place"),UIImageMake(@"banner_place")];
    }
    return _bannerView;
}
- (UICollectionView *)homeCollectionView{
    if (!_homeCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 0.5;
        layout.minimumInteritemSpacing = 0.5;
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        _homeCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _homeCollectionView.delegate = self;
        _homeCollectionView.dataSource = self;        
        _homeCollectionView.backgroundColor = [UIColor whiteColor];
    }
    return _homeCollectionView;
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [_homeCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bannerView.mas_bottom).offset(1);
        make.left.right.bottom.equalTo(self.view);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
